package com.yiyang.controller;


import com.yiyang.exce.BizException;
import com.yiyang.pojo.vo.Result;
import com.yiyang.utils.AliyunOSSUtils;
import com.yiyang.utils.BizCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/upload")
@Tag(name = "UploadController",description = "文件上传")
public class UploadController {
    @PostMapping
    public Result<String> upload(@RequestParam("file") MultipartFile multipartFile){
        String filename = multipartFile.getOriginalFilename();
        try{
            // 使用JDK提供的UUID类，获取伪随机数串
            UUID uuid = UUID.randomUUID();
            // 获取原有文件名的扩展名
            String substring = filename.substring(filename.lastIndexOf("."));
            filename = uuid.toString() + substring;
            // 获取到上传的文件数据
            String s = AliyunOSSUtils.uploadFile(multipartFile.getInputStream(), filename);
            return Result.success(BizCode.SUCCESS,s);
        }catch (Exception e){
            throw new BizException(BizCode.FILE_UPLOAD_ERROR);
        }
    }
}
